/*
 * All rights Reserved, Designed By www.goldbeacon.cn
 * @Title:  demo
 * @Package com.example.demo.web
 * @author: zoujie
 * @date:   2022/8/1616:26
 * @version V1.0
 * Copyright © 2022 www.goldbeacon.cn. All Rights Reserved.
 * 注意：本内容仅限于湖南金烽信息科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.chenzhengrong.demospringboot.controlloer;

import com.chenzhengrong.demospringboot.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @author zoujie
 * @title: UserController
 * @description: 用户管理控制器
 * @date 2022/8/16 16:26
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/users")
public class UserController {

    /** 创建线程安全的Map-模拟用户数据表 */
    private Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());

    /**
     * @return java.util.List<cn.goldbeacon.demo.domain.User>
     * @description: 处理"/users/"的GET请求，⽤来获取⽤户列表
     * 还可以通过@RequestParam从⻚⾯中传递参数来进⾏查询条件或者翻⻚信息的传递
     * @author zoujie
     * @date 2021/7/20 18:28
     */
    @ApiOperation(value = "获取用户列表")
    @RequestMapping(value="/", method= RequestMethod.GET)
    public List<User> getUserList() {
        return new ArrayList<User>(users.values());
    }

    /**
     * @description: 处理"/users/"的POST请求，⽤来创建User
     * 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从⻚⾯中传递参数
     * @param user
     * @return java.lang.String
     * @author zoujie
     * @date 2021/7/20 18:29
     */
    @RequestMapping(value="/", method=RequestMethod.POST)
    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    public String postUser(@ModelAttribute User user) {
        users.put(user.getId(), user);
        return "success";
    }

    /**
     * @description: 处理"/users/{id}"的GET请求，⽤来获取url中id值的User信息
     * url中的id可通过@PathVariable绑定到函数的参数中
     * @param id 用户id
     * @return cn.goldbeacon.demo.domain.User
     * @author zoujie
     * @date 2021/7/20 18:29
     */
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    /**
     * @description: 处理"/users/{id}"的PUT请求，⽤来更新User信息
     * @param id 用户id
     * @param user 用户数据
     * @return java.lang.String
     * @author zoujie
     * @date 2021/7/20 18:30
     */
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    @ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户编号", required = true, example = "1")
    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    public String putUser(@PathVariable Long id, @ModelAttribute User user) {
        User data = users.get(id);
        data.setName(user.getName());
        data.setAge(user.getAge());
        users.put(id, data);
        return "success";
    }

    /**
     * @description: 处理"/users/{id}"的DELETE请求，⽤来删除User
     * @param id 用户id
     * @return java.lang.String
     * @author zoujie
     * @date 2021/7/20 18:32
     */
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }

    /**
     * @description: 测试valid
     * @param user 用户数据
     * @return java.lang.String
     * @author zoujie
     * @date 2021/7/23 11:37
     */
    @RequestMapping(value="/valid", method=RequestMethod.POST)
    @ApiOperation(value = "测试valid", notes = "测试valid用法")
    public String valid(@Valid @RequestBody User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "failed: " + bindingResult.getFieldError().getDefaultMessage();
        }
        return "success";
    }
}
